#!/usr/bin/env bash
# group: img
#
# qemu-img measure sub-command tests
#
# Copyright (C) 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=stefanha@redhat.com

seq=`basename $0`
echo "QA output created by $seq"

status=1    # failure is the default!

_cleanup()
{
    _cleanup_test_img
    _rm_test_img "$TEST_IMG.converted"
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ./common.rc
. ./common.filter
. ./common.pattern

_supported_fmt raw qcow2
_supported_proto file
_supported_os Linux
_require_working_luks

echo "== Input validation =="
echo

_make_test_img 1G

$QEMU_IMG measure # missing arguments
$QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed
$QEMU_IMG measure "$TEST_IMG" a # only one filename allowed
$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # size or filename needed
$QEMU_IMG measure --image-opts # missing filename
$QEMU_IMG measure -f qcow2 # missing filename
$QEMU_IMG measure -l snap1 # missing filename
$QEMU_IMG measure -o , # invalid option list
$QEMU_IMG measure -l snapshot.foo=bar # invalid snapshot option
$QEMU_IMG measure --output foo # invalid output format
$QEMU_IMG measure --size -1 # invalid image size
$QEMU_IMG measure -O foo "$TEST_IMG" # unknown image file format

make_test_img_with_fmt() {
    # Shadow global variables within this function
    local IMGFMT="$1"
    _make_test_img --no-opts "$2"
}

qemu_io_with_fmt() {
    # Shadow global variables within this function
    local QEMU_IO_OPTIONS=$(echo "$QEMU_IO_OPTIONS" | sed "s/-f $IMGFMT/-f $1/")
    shift
    $QEMU_IO "$@"
}

# The proof is in the pudding: converted image size cannot be larger than the
# required size.
#
# Note: if a change to the image format code causes the file size to change,
# then this test fails!  This is good because it's a reminder to check that the
# required size is still at least as big as the actual converted file size.
convert_and_show_size() {
    local fmt="$1"
    shift
    $QEMU_IMG convert -f "$fmt" -O "$IMGFMT" "$TEST_IMG" "$@" "$TEST_IMG.converted"
    stat -c "converted image file size in bytes: %s" "$TEST_IMG.converted"
}

for ofmt in human json; do
    echo
    echo "== Size calculation for a new file ($ofmt) =="
    echo

    # Try a few interesting sizes
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 0
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 64G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 256G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 1T
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2P
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 7E

    # Always test the raw input files but also IMGFMT
    for fmt in $(echo -e "raw\n$IMGFMT\n" | sort -u); do
        echo
        echo "== Empty $fmt input image ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 0
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"

        echo
        echo "== $fmt input image with data ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 1G
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        qemu_io_with_fmt "$fmt" -c "write 512 512" "$TEST_IMG" | _filter_qemu_io
        qemu_io_with_fmt "$fmt" -c "write 64K 64K" "$TEST_IMG" | _filter_qemu_io
        if [ "$fmt" = "qcow2" ]; then
            $QEMU_IMG snapshot -c snapshot1 "$TEST_IMG"
        fi
        qemu_io_with_fmt "$fmt" -c "write 128M 63K" "$TEST_IMG" | _filter_qemu_io
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"

        if [ "$fmt" = "qcow2" ]; then
            echo
            echo "== $fmt input image with internal snapshot ($ofmt) =="
            echo
            $QEMU_IMG measure --output=$ofmt -f "$fmt" -l snapshot1 \
                              -O "$IMGFMT" "$TEST_IMG"
            echo
            convert_and_show_size "$fmt" -l snapshot1
        fi

        if [ "$IMGFMT" = "qcow2" ]; then
            echo
            echo "== $fmt input image and a backing file ($ofmt) =="
            echo
            # The backing file doesn't need to exist :)
            $QEMU_IMG measure --output=$ofmt -o backing_file=x \
                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"

            echo
            echo "== $fmt input image and LUKS encryption =="
            echo
            $QEMU_IMG measure --output=$ofmt \
                              --object secret,id=sec0,data=base \
                              -o encrypt.format=luks,encrypt.key-secret=sec0,encrypt.iter-time=10 \
                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        fi

        echo
        echo "== $fmt input image and preallocation ($ofmt) =="
        echo
        $QEMU_IMG measure --output=$ofmt -o preallocation=full \
                          -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt" -o preallocation=full

        echo
        echo "== Fully-allocated $fmt input image ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 8M
        qemu_io_with_fmt "$fmt" -c "write 0 8M" "$TEST_IMG" | _filter_qemu_io
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"
    done
done

# success, all done
echo "*** done"
rm -f $seq.full
status=0
